package com.jtjrenren.android.taxi.passenger.core.app.splash;

import com.jtjrenren.android.taxi.passenger.bean.api.ApiUpVersion;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.library.mvp.presenter.BasePresenter;
import com.library.utils.LogUtil;
import com.library.utils.SpUtils;

/**
 * Created by Administrator on 2016/9/9.
 */

public class SplashPresenterImpl extends BasePresenter<SplashView, SplashInteractor> implements SplashPresenter {
    public SplashPresenterImpl(SplashView view) {
        super(view);
    }

    @Override
    public SplashInteractor createInteractor() {
        return new SplashInteractorImpl();
    }

    @Override
    public void onSub() {
    }

    @Override
    public void unSub() {
    }


    @Override
    public void downApp(String url) {
        LogUtil.log("dwon", "开始下载app");
        interactor.downApp(url, new SplashInteractor.DownListenr() {
            @Override
            public void downing(int process) {
                view.refreshDownProcess(process);
            }

            @Override
            public void finish(String path) {
                view.downFinish(path);
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public void initAPP() {
        interactor.checkVersion(new ApiWorks.ResponseListener<ApiUpVersion.VersionEntity>() {
            @Override
            public void onResponse(ApiUpVersion.VersionEntity versionEntity) {

                if (versionEntity != null) {
                    String url = versionEntity.getAndroidurl();
                    String desc = versionEntity.getContent();
                    if (versionEntity.getIsUpdate() == 1)
                        view.showUpDialog(url, desc, true);
                    else
                        view.showUpDialog(url, desc, false);
                } else {
                    doUpNext();
                }
            }
        });
        AppManger.getInstance().refreshConfig();
    }

    @Override
    public void doUpNext() {
        if (AppManger.getInstance().isLauched()) {
            view.showMainPage();
            if (AppManger.getInstance().getRemberUser() != null)
                AppManger.getInstance().loginSuccess(AppManger.getInstance().getRemberUser());
        } else {
            view.showGuidePage();
        }
        SpUtils.setParam(AppManger.getInstance().getContext(), "lauchcount", 1);
    }
}
